/* 
         Name : TriangleConvert.c 
   Programmer : Kambiz Ghazinour 
Creation Date : 2006-12-05 
Last Modified : 2007-10-25 
         Goal : Converts Element file to a acceptable file by 
mesh2nodal function in METIS that creates graph  
 
      Compile : cc -lm -o TriangleConvert.out TriangleConvert.c 
          Run :./TriangleConvert.out  
 
-----------------Data Dictionary----------------- 
 
         ElSz : Element matrix size 
         ndof : Number of degree of freedom per node 
  MaxLineSize : Maximum length of line that will be read from the files 
iFileLineItem : Number of items that should be read in each file’s line 
*/ 
 
#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
#include <time.h> 
#include <math.h> 
 
#define MaxLineSize 150 
#define ElSz 4 
#define ndof 1 
#define TempSz 1000  
 
int iNEl, iNNel, iNNode, iDim, sdof, iNBound; 
int convProc,convTotal,StopFlag; 
 
int Index[ElSz]; 
 
 
double *SplitLine(char sLine[], double *arSplitLine) 
        { 
        #define DLMT " " 
         
        int i = 0; 
        char *tokenPtr; 
         
        tokenPtr = strtok(sLine, DLMT); 
        while (tokenPtr != NULL) 
                { 
                arSplitLine[i] = atof(tokenPtr); 
                tokenPtr = strtok(NULL, DLMT);   
                i++; 
                }    
        arSplitLine[i] = -i; 
        return arSplitLine;  
        }
int Convert(int argc, char **argv) 
  {   
  int i, j, k, iTemp; 
  int iIndex; 
  int t; 
  int z; 
  int Weight,Bmark; 
 
  char sLine[MaxLineSize]; 
  char *chrPut[MaxLineSize];  
       char *tokenPtr; 
 
  FILE *stream1; 
  FILE *stream2; 
       int  kFlag, kTemp, kProc; 
       char *sNodes; 
         char str[MaxLineSize]; 
                          
             
     /* Parse program args to get input file names, and open them */ 
         if (argc < 1) 
                 { 
                 printf("Usage: %s <nodenum>\n", argv[0]); 
                 exit(EXIT_SUCCESS); 
                 } 
 
            sNodes = argv[1]; 
                 
            sprintf(sLine, "./Input/Element%s",sNodes); 
            stream1 = fopen(sLine, "r"); 
 
            sprintf(sLine, "./Input/Mesh%s",sNodes); 
            stream2 = fopen(sLine, "wb"); 
 
  fseek(stream1, 0, SEEK_SET); 
  fseek(stream2, 0, SEEK_SET); 
 
  fgets(sLine, MaxLineSize, stream1); 
  sscanf(sLine,"%d",&iNEl); 
 
  sprintf(sLine, "%d  1 \n",iNEl); 
       fputs(sLine,stream2);      
 
  for (i = 1; i <= iNEl; i++) 
    { 
    fgets(sLine, MaxLineSize, stream1); 
           tokenPtr = strtok(sLine, " "); 
    iIndex=0; 
    while (tokenPtr != NULL) 
                  { 
                  chrPut[iIndex] = tokenPtr; 
                  tokenPtr = strtok(NULL, " "); 
                  iIndex++; 
                  } 
 
   sprintf(sLine, "%s %s %s",chrPut[1],chrPut[2], 
chrPut[3]); 
   fputs(sLine,stream2);    }
} 
 
int main(int argc, char **argv) 
 { 
 Convert(argc,argv); 
 return 0;
 }
